package me.miencraft.plugin.superlobby.config;

import lombok.Getter;
import me.miencraft.plugin.superlobby.SuperLobby;
import me.miencraft.plugin.superlobby.item.HideItem;
import me.miencraft.plugin.superlobby.item.ItemProperties;
import me.miencraft.plugin.superlobby.item.JoinItem;
import me.miencraft.plugin.superlobby.item.VisibleItem;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

@Getter
public class MainConfig extends BaseConfig {

    private int defaultSlot;
    private int hideItemSlot;
    private HideItem hideItem;
    private VisibleItem visibleItem;
    private List<JoinItem> joinItemList;

    public MainConfig(JavaPlugin plugin) {
        super(plugin, "config.yml");
        saveDefault();
        load();
    }

    @Override
    public void load() {
        super.load();
        defaultSlot = getInt("default_slot");
        hideItemSlot = getInt("visible_item.slot");
        hideItem = new HideItem(getItemProperties("visible_item.hide_item"));
        visibleItem = new VisibleItem(getItemProperties("visible_item.visible_item"));
        ConfigurationSection section;
        section = getConfigurationSection("join_item");
        joinItemList = new ArrayList<>();
        if (section != null) {
            for (String key : section.getKeys(false)) {
                int slot;
                try {
                    slot = Integer.parseInt(key);
                } catch (NumberFormatException e) {
                    SuperLobby.instance.getLogger().warning("Invalid join item slot: " + key);
                    continue;
                }
                ItemProperties properties = getItemProperties("join_item." + key);
                List<String> list = getStringList("join_item." + key + ".execute");
                joinItemList.add(new JoinItem(properties, slot, list));
            }
        }
    }

    @NotNull
    public ItemProperties getItemProperties(@NotNull String path) {
        Material material = getMaterial(path + ".material");
        String name = getString(path + ".name");
        List<String> lore = getStringList(path + ".lore");
        return new ItemProperties(material, name, lore.isEmpty() ? null : lore);
    }

}